package com.tkjt.pms.modules.kpiAudit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.calc.core.AuditStatusWatcher;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueAttrs;
import com.tkjt.pms.common.entity.TkIndexValueAudit;
import com.tkjt.pms.common.utils.DepartCode;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.MyBeanUtils;
import com.tkjt.pms.common.utils.TkIndexUtils;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAttrsMapper;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.common.service.TkIndexValueAuditService;
import com.tkjt.pms.modules.kpiAudit.mapper.KpiAuditMapper;
import com.tkjt.pms.modules.kpiAudit.model.kpiAuditrowList.KpiAuditrowListModel;
import com.tkjt.pms.modules.kpiAudit.model.kpiAuditrowList.YearModel;
import com.tkjt.pms.modules.kpiAudit.model.vo.AuditStatisticsVO;
import com.tkjt.pms.modules.kpiAudit.model.vo.TkIndexValueAuditVO;
import com.tkjt.pms.modules.kpiAudit.service.KpiAuditBizService;
import com.tkjt.pms.modules.kpiAudit.service.KpiAuditService;
import com.tkjt.pms.modules.kpiAudit.thread.MakeMirrorsThread;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Transactional
@Service
public abstract class KpiAuditBizServiceBase extends ServiceImpl<KpiAuditMapper, TkIndexValueAudit> implements KpiAuditBizService {

    @Autowired
    ISysDictItemService sysDictItemService;
    @Autowired
    TkIndexValueAuditService tkIndexValueAuditService;
    @Autowired
    private TkDataConfigService tkDataConfigService;
    @Autowired
    TkIndexValueAttrsMapper tkIndexValueAttrsMapper;
    @Autowired
    private AuditStatusWatcher auditStatusWatcher;
    
    @Override
    public KpiAuditrowListModel getKpiAuditrowListModel(HttpServletRequest request,String dataType) {
        KpiAuditrowListModel model=new KpiAuditrowListModel();
        model.setDefaulYearMonth(EsDateUtils.getIndexYearMonth(new Date()));
        model.setYearMonthList(getYearMonthList());
        model.setTkIndexValueAuditVOList(getTkIndexValueAuditVOList(request,dataType));
        return model;
    }

    @Override
    public List<AuditStatisticsVO> getAuditStatisticsVOList(HttpServletRequest request,String dataType) {
        String indexGroup = request.getParameter("indexGroup");//A01A01A01
        if (null != indexGroup && !"".equals(indexGroup)) {
            String indexGroupCode="A01A01A01".equals(indexGroup)?"G":"C";//2  一线 1 本部
            List<TkIndexInfo> singleList = baseMapper.getSingleList(indexGroupCode);
            Map<String,Object> param = new HashMap<>();
            param.put("indexGroupCode",indexGroupCode);
            param.put("dataType",dataType);
            List<TkIndexValue> list = baseMapper.getTkIndexValueByIndexGroup(param);

            List<AuditStatisticsVO> finalList = new ArrayList<>();
            for (TkIndexInfo indexInfo : singleList) {
                List<TkIndexValue> tempList = new ArrayList<>();
                String indexValueCode = indexInfo.getIndexValueCode();
                for (TkIndexValue itemEntity : list) {
                    if (indexValueCode.equals(itemEntity.getIndexValueCode())) {
                        tempList.add(itemEntity);
                    }
                }
                AuditStatisticsVO auditStatisticsVO = new AuditStatisticsVO();
                auditStatisticsVO.setIndexCode(indexInfo.getIndexCode());
                auditStatisticsVO.setIndexValueCode(indexInfo.getIndexValueCode());
                TkIndexInfo tempInfo= baseMapper.getTkIndexInfoByIndexValueCode(indexInfo.getIndexValueCode());
                auditStatisticsVO.setIndexCodeName(tempInfo.getIndexName());
                auditStatisticsVO.setIndexValueCodeName(tempInfo.getIndexValueName());
                for (TkIndexValue temp : tempList) {
                    switch (temp.getIndexDepart()) {
                        case DepartCode.YANYUAN_SHEQU:
                            auditStatisticsVO.setData1(makeGradeString(temp));
                            break;
                        case DepartCode.YANYUAN_YIYUAN:
                            auditStatisticsVO.setData2(makeGradeString(temp));
                            break;
                        case DepartCode.SHENYUAN_SHEQU:
                            auditStatisticsVO.setData3(makeGradeString(temp));
                            break;
                        case DepartCode.SHENYUAN_YIYUAN:
                            auditStatisticsVO.setData4(makeGradeString(temp));
                            break;
                        case DepartCode.YUEYUAN_SHEQU:
                            auditStatisticsVO.setData5(makeGradeString(temp));
                            break;
                        case DepartCode.YUEYUAN_YIYUAN:
                            auditStatisticsVO.setData6(makeGradeString(temp));
                            break;
                        case DepartCode.SHUYUAN_SHEQU:
                            auditStatisticsVO.setData7(makeGradeString(temp));
                            break;
                        case DepartCode.SHUYUAN_YIYUAN:
                            auditStatisticsVO.setData8(makeGradeString(temp));
                            break;

                        case DepartCode.WUYUAN_SHEQU:
                            auditStatisticsVO.setData9(makeGradeString(temp));
                            break;
                        case DepartCode.WUYUAN_YIYUAN:
                            auditStatisticsVO.setData10(makeGradeString(temp));
                            break;
                        case DepartCode.CHUYUAN_SHEQU:
                            auditStatisticsVO.setData11(makeGradeString(temp));
                            break;
                        case DepartCode.CHUYUAN_YIYUAN:
                            auditStatisticsVO.setData12(makeGradeString(temp));
                            break;
                        case DepartCode.YUANQU_HEJI:
                            auditStatisticsVO.setData13(makeGradeString(temp));
                            break;

                        case DepartCode.SHIYEQUN:
                            auditStatisticsVO.setData1(makeGradeString(temp));
                            break;
                        case DepartCode.SHICHANGYINGXIAO:
                            auditStatisticsVO.setData2(makeGradeString(temp));
                            break;
                        case DepartCode.FUWUGUANLI:
                            auditStatisticsVO.setData3(makeGradeString(temp));
                            break;
                        case DepartCode.CANYINGUANLI:
                            auditStatisticsVO.setData4(makeGradeString(temp));
                            break;
                        case DepartCode.YUNXINGBAOZHANG:
                            auditStatisticsVO.setData5(makeGradeString(temp));
                            break;
                        case DepartCode.ZHAOHUZHUANYE:
                            auditStatisticsVO.setData6(makeGradeString(temp));
                            break;
                        case DepartCode.YIYUANFAZHAN_GUANLI:
                            auditStatisticsVO.setData7(makeGradeString(temp));
                            break;
                        case DepartCode.CANWUGUANLI:
                            auditStatisticsVO.setData8(makeGradeString(temp));
                            break;
                        case DepartCode.JINGYINGGUANLI:
                            auditStatisticsVO.setData9(makeGradeString(temp));
                            break;
                        case DepartCode.XINGZHENGBANGONGSHI:
                            auditStatisticsVO.setData10(makeGradeString(temp));
                            break;
                        case DepartCode.RENLIZIYUAN:
                            auditStatisticsVO.setData11(makeGradeString(temp));
                            break;
                        case DepartCode.YEWUFAZHAN:
                            auditStatisticsVO.setData12(makeGradeString(temp));
                            break;
                        case DepartCode.SHIYEQUN_BENBU:
                            auditStatisticsVO.setData13(makeGradeString(temp));
                            break;

                        default:
                            break;
                    }
                }
                finalList.add(auditStatisticsVO);
            }
            return finalList;
        } else {

        }
        return null;
    }

    @Override
    public String toSubmitAudit(HttpServletRequest request,String dataType) {
        String message = null;
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        String auditComments = request.getParameter("auditComments");
        TkIndexValueAudit tkIndexValueAuditEntity = baseMapper.getTkIndexValueAuditById(id);
        if (tkIndexValueAuditEntity != null) {
            try {
                if (status == null || "".equals(status)) {
                    message = "审核提交成功";
                    tkIndexValueAuditEntity.setAuditStatus("2");
                    tkIndexValueAuditEntity.setSubmitTime(new Date());
                    baseMapper.updateById(tkIndexValueAuditEntity);
//                    tkIndexValueAuditService.saveOrUpdate(tkIndexValueAuditEntity);
                } else {
                    tkIndexValueAuditEntity.setAuditComments(auditComments);
                    if ("Y".equals(status)) {
                        message = "正在生成镜像数据";
                        String mirrorCode = TkIndexUtils.getMirrorCode(new Date());
                        new Thread(new MakeMirrorsThread(mirrorCode, tkIndexValueAuditEntity.getId(),dataType,tkIndexValueAuditService,auditStatusWatcher)).start();
                    } else if ("N".equals(status)) {
                        message = "审核退回成功";
                        tkIndexValueAuditEntity.setAuditStatus("4");
                        tkIndexValueAuditEntity.setAuditTime(new Date());
                        baseMapper.updateById(tkIndexValueAuditEntity);
//                        tkIndexValueAuditService.saveOrUpdate(tkIndexValueAuditEntity);
                    }
                }
                // message = tkIndexValueAuditService.submitAudit(status, auditComments, tkIndexValueAuditEntity);
                /// systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
            } catch (Exception e) {
                e.printStackTrace();
                message = "审核失败";
            }
        } else {
            message = "数据不存在";
        }
        return message;
    }



    public String makeGradeString(TkIndexValue temp) {
        QueryWrapper<TkIndexValueAttrs> attrsWrapper = new QueryWrapper<>();
        attrsWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexFormat");
        attrsWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,temp.getIndexValueCode());
        TkIndexValueAttrs tkIndexValueAttrs = tkIndexValueAttrsMapper.selectOne(attrsWrapper);
        if (tkIndexValueAttrs != null && "3".equals(tkIndexValueAttrs.getAttrValue())) {
            return DepartCode.changeFormate(temp.getMTotal());
        }
        return DepartCode.formateForDouble(temp.getMTotal()).toString();
    }
    private List<YearModel> getYearMonthList(){
//        SysDict typeList =sysDictService.getOne(new QueryWrapper<SysDict>().eq("dict_code","yearmonth"));
//        List<SysDict> typeList=baseMapper.getTsTypeByCode("yearmonth");
        List<SysDictItem> typeList=sysDictItemService.selectItemsByMainId("yearmonth");
        List<YearModel> list=new ArrayList<>();
        for (SysDictItem t:typeList){
            YearModel model=new YearModel();
            model.setOption(t.getItemText());
            model.setValue(t.getItemValue());
            list.add(model);
        }
        return list;
    }

    private List<TkIndexValueAuditVO> getTkIndexValueAuditVOList(HttpServletRequest request,String dataType){
        //第一次进入页面需要前端自己生成当前月份日期
        String yearmonth = request.getParameter("yearMonth");
        Map<String,String> queryMap=new HashMap<>();
        if (null != yearmonth && !"".equals(yearmonth)) {
            queryMap.put("indexValueY",yearmonth.substring(0, 4));
            queryMap.put("indexValueM",yearmonth.substring(4));
        }
        queryMap.put("dataType",dataType);
        List<TkIndexValueAudit> list = baseMapper.getTkIndexValueAuditByYM(queryMap);
        List<TkIndexValueAuditVO> finalList = new ArrayList<>();
        try {
            for (TkIndexValueAudit item : list) {
                TkIndexValueAuditVO tkIndexValueAuditVO = new TkIndexValueAuditVO();
                MyBeanUtils.copyBeanNotNull2Bean(item, tkIndexValueAuditVO);
                if(item.getAuditTime() != null){
                    tkIndexValueAuditVO.setAuditTime(EsDateUtils.format(item.getAuditTime()));
                }
                if(item.getSubmitTime() != null){
                    tkIndexValueAuditVO.setSubmitTime(EsDateUtils.format(item.getSubmitTime()));
                }
                tkIndexValueAuditVO.setYearmonth(tkIndexValueAuditVO.getIndexValueY() + tkIndexValueAuditVO.getIndexValueM());
                tkIndexValueAuditVO.setIndexGroupName(baseMapper.getTsDepartByOrgCode(tkIndexValueAuditVO.getIndexGroup()));
                tkIndexValueAuditVO.setIndexValueM(EsDateUtils.getMonthToUp(Integer.valueOf(tkIndexValueAuditVO.getIndexValueM())));
                tkIndexValueAuditVO.setAuditStatus(tkIndexValueAuditVO.getAuditStatusByStr(Integer.valueOf(tkIndexValueAuditVO.getAuditStatus())));
                finalList.add(tkIndexValueAuditVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalList;
    }
}
